﻿using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using MedLink2011.Views.Patients.ViewModels;
using MedLink2011.Web.Services;

namespace MedLink2011.Views.Patients.ChildWindows
{
    public partial class LabsAddCW : ChildWindow
    {
        
        readonly int loadedPatient_Id = MedLink2011.Views.Patients.ViewModels.PatientViewModel.DashLoad_PK;
        //private int loadedPatient_Id = 1;
        private EntityQuery<MedLink2011.Web.Models.Doctor> _doctorQuery;
        private EntityQuery<MedLink2011.Web.Models.Exam> _examQuery;
        private EntityQuery<MedLink2011.Web.Models.Diagno> _diagnoQuery;
        private MedLink2011Context _context;
        
        public LabsAddCW()
        {
            InitializeComponent();
        LoadComboBoxData();
            
        }


        private void OKButton_Click(object sender, RoutedEventArgs e)
            {
                Patient_IdTextBox.Text = loadedPatient_Id.ToString(); //make sure is not the primary key of the current form, it needs to be a FOREIGN key.
                Doctor_IdTextBox.Text = Doctor_IdComboBox.SelectedIndex.ToString();
                Exam_idTextBox.Text = Exam_idComboBox.SelectedIndex.ToString();
                Diagno_IdTextBox.Text = Diagno_IdComboBox.SelectedIndex.ToString();
            if (!LabViewModel._labContext.IsLoading)
                {
                    this.LabsDataForm.CommitEdit();
                    this.DialogResult = true;
                }
            }
            
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        
        private void LoadComboBoxData()
        {

            //Doctor_IdComboBox
             _context = new MedLink2011Context();
            _doctorQuery = _context.GetDoctorsQuery();
            _doctorQuery = _doctorQuery.OrderBy(e => e.Doctor_Id);
            _context.Load(_doctorQuery, LoadBehavior.RefreshCurrent, Doctor_IdComboBox_Loaded, null);
            Doctor_IdComboBox.ItemsSource = _context.Doctors;
            //Exam_idComboBox
             _context = new MedLink2011Context();
            _examQuery = _context.GetExamsQuery();
            _examQuery = _examQuery.OrderBy(e => e.Exam_id);
            _context.Load(_examQuery, LoadBehavior.RefreshCurrent, Exam_idComboBox_Loaded, null);
            Exam_idComboBox.ItemsSource = _context.Exams;
            //Diagno_IdComboBox
             _context = new MedLink2011Context();
            _diagnoQuery = _context.GetDiagnosQuery();
            _diagnoQuery = _diagnoQuery.OrderBy(e => e.Diagno_Id);
            _context.Load(_diagnoQuery, LoadBehavior.RefreshCurrent, Diagno_IdComboBox_Loaded, null);
            Diagno_IdComboBox.ItemsSource = _context.Diagnos;
        }


            private void Doctor_IdComboBox_Loaded(LoadOperation<MedLink2011.Web.Models.Doctor> lo)
            {
                this.Doctor_IdComboBox.SelectedIndex = 1;
            }
            private void Exam_idComboBox_Loaded(LoadOperation<MedLink2011.Web.Models.Exam> lo)
            {
                this.Exam_idComboBox.SelectedIndex = 1;
            }
            private void Diagno_IdComboBox_Loaded(LoadOperation<MedLink2011.Web.Models.Diagno> lo)
            {
                this.Diagno_IdComboBox.SelectedIndex = 1;
            }

            private void LabsDataForm_EditEnded(object sender, DataFormEditEndedEventArgs e)
            {
                this.Close();
            }

    }
}

